#include "database.hpp"
#include "queries.hpp"

#include <QtCore/QVariant>

/*---------------------------------------------------------------------------*/

namespace DatabaseManager {

/*---------------------------------------------------------------------------*/


Database::Database()
{
	m_database = QSqlDatabase::addDatabase( "QSQLITE" );
}


/*---------------------------------------------------------------------------*/


Database::~Database()
{
	close();
}


/*---------------------------------------------------------------------------*/


void
Database::open( const char* _databaseName )
{
	close();

	m_database.setDatabaseName( _databaseName );
	m_database.open();
	m_database.exec( Query::CreateVCSTable );
}


/*---------------------------------------------------------------------------*/


void
Database::close()
{
	if ( m_database.isOpen() )
		m_database.close();
}


/*---------------------------------------------------------------------------*/


void
Database::insertVCS( const char* _url, const char* _username, const char* _password )
{
	if ( !m_insertVcsQuery )
	{
		m_insertVcsQuery = m_database.exec();
		m_insertVcsQuery->prepare( Query::InsertVCS );
	}
	m_insertVcsQuery->bindValue( ":url", _url );
	m_insertVcsQuery->bindValue( ":username", _username );
	m_insertVcsQuery->bindValue( ":password", _password );
	m_insertVcsQuery->exec();
}


/*---------------------------------------------------------------------------*/

} // namespace DatabaseManager
